<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => (formState = {})">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item :label="isArmy ? '登记日期' : '挂号日期'" name="dateRange" class="w-72">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="pyjm" class="w-72 ml-2">
          <a-input v-model:value="formState.pyjm" placeholder="姓名/卡号/助记码/手机号/身份证号" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <BaseTable :loading="loading" column-code="00001173" fill :data-source="tableData" bordered :scroll="{ x: 2200 }" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record.mc, gg: record.gg }" />
          </template>
          <template v-if="column.dataIndex === 'zxzt'">
            <a-tag v-if="record.zxzt !== 0" color="success">已执行</a-tag>
            <a-tag v-else color="blue">未执行</a-tag>
          </template>

          <template v-if="column.dataIndex === 'action'">
            <a-button v-if="!record.zxzt" type="link" size="small" @click="onClickExecute(record.id)"> 强制执行 </a-button>
            <BaseButton v-if="record.zxzt" confirm-text="确认撤销?" type="link" danger size="small" @click="onClickQuash(record)">强制撤销</BaseButton>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { post, get } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import BaseButton from '@/components/BaseButton'

const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const isArmy = ref(false)

const onClickQuash = async (record) => {
  const res = await get('/outp/mzhs/doCancelExecuteClinicalOrder?yzid=' + record.id)
  if (res.code !== 0) return message.error(res.msg || '操作失败')
  message.success(res.msg || '操作成功')
  getData()
}
const onClickExecute = async (id) => {
  const { code, msg } = await post('/outp/mzhs/doExecuteClinicalOrder', [id])
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}
const { loading, tableData, getData, pagination } = useTable({
  url: '/outp/mzhs/getOutpClinicalOrderInfoList',
  formatParams: () => {
    return {
      status: '1',
      tclb: '1',
      sfyzfyz: '0',
      stdate: formState.value.dateRange?.[0],
      eddate: formState.value.dateRange?.[1],
      ...formState.value
    }
  }
})

onMounted(() => {
  getData()
  isArmy.value = getSystemConfig('NAVYHIS_MODE') === '1' //军用系统
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
